package com.jeomo.sys.provider.web;

import com.jeomo.base.provider.controller.BaseController;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.vo.PageResponseVo;
import com.jeomo.sys.provider.request.CreateDictRequest;
import com.jeomo.sys.provider.web.vo.DictTypeVo;
import com.jeomo.sys.provider.web.vo.DictVo;
import com.jeomo.sys.starter.entity.Dict;
import com.jeomo.sys.starter.service.IDictService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author QBT
 */
@RequestMapping("/sys/dict")
@RestController
public class DictController extends BaseController {

    @Resource
    private IDictService dictService;

    @GetMapping("all")
    @ApiOperation("分页查询")
    public CommonResult<List<DictVo>> all() {
        List<Dict> dicts = dictService.list();
        return CommonResult.success(BeanCopyUtil.copyListProperties(dicts, DictVo::new));
    }


    @GetMapping("type/all")
    @ApiOperation("分页查询")
    public CommonResult<List<String>> allType() {
        return CommonResult.success(dictService.queryAllType());
    }

    @PostMapping("page")
    @ApiOperation("分页查询")
    public CommonResult<PageResponseVo<DictVo>> page(@RequestBody @ApiParam("分页查询参数") PageRequest pageRequest) {
        PageQueryDto dto = BeanCopyUtil.copyProperties(pageRequest, PageQueryDto.class);
        return CommonResult.success(BeanCopyUtil.copyPageProperties(dictService.pageSearch(dto), DictVo.class));
    }


    @GetMapping("{typeCode}/{code}")
    public CommonResult<DictVo> query(@PathVariable String typeCode, @PathVariable String code) {
        return CommonResult.success(BeanCopyUtil.copyProperties(dictService.queryByCode(typeCode, code), DictVo.class));
    }

    @PostMapping("create")
    @ApiOperation("新建字典")
    public CommonResult<DictTypeVo> create(@RequestBody CreateDictRequest request) {
        Dict dict = BeanCopyUtil.copyProperties(request, Dict.class);
        dictService.save(dict);
        return CommonResult.success(BeanCopyUtil.copyProperties(dict, DictTypeVo.class));
    }

    @DeleteMapping("{typeCode}/{code}")
    @ApiOperation("删除字典")
    public CommonResult<Boolean> remove(@PathVariable String typeCode, @PathVariable String code) {
        dictService.remove(typeCode, code);
        return CommonResult.success(Boolean.TRUE);
    }

}
